package com.ruoyi.project.channel.channelUserInfo.controller;

import java.util.List;

import com.ruoyi.common.utils.Md5Utils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.channelUserInfo.domain.ChannelUserInfo;
import com.ruoyi.project.channel.channelUserInfo.service.IChannelUserInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 码商代理扩展信息操作处理
 * 
 * @author CAIN
 * @date 2019-10-29
 */
@Controller
@RequestMapping("/channel/channelUserInfo")
public class ChannelUserInfoController extends BaseController
{
    private String prefix = "channel/channelUserInfo";
	
	@Autowired
	private IChannelUserInfoService channelUserInfoService;
	
	@RequiresPermissions("channel:channelUserInfo:view")
	@GetMapping()
	public String channelUserInfo()
	{
	    return prefix + "/channelUserInfo";
	}
	
	/**
	 * 查询码商代理扩展列表
	 */
	@RequiresPermissions("channel:channelUserInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ChannelUserInfo channelUserInfo)
	{
		startPage();
        List<ChannelUserInfo> list = channelUserInfoService.selectChannelUserInfoList(channelUserInfo);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出码商代理扩展列表
	 */
	@RequiresPermissions("channel:channelUserInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelUserInfo channelUserInfo)
    {
    	List<ChannelUserInfo> list = channelUserInfoService.selectChannelUserInfoList(channelUserInfo);
        ExcelUtil<ChannelUserInfo> util = new ExcelUtil<ChannelUserInfo>(ChannelUserInfo.class);
        return util.exportExcel(list, "channelUserInfo");
    }
	
	/**
	 * 新增码商代理扩展
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 修改码商代理扩展
	 */
	@GetMapping("/edit/{channelUserId}")
	public String edit(@PathVariable("channelUserId") Long channelUserId, ModelMap mmap)
	{
		ChannelUserInfo channelUserInfo = channelUserInfoService.selectChannelUserInfoById(channelUserId);
		mmap.put("channelUserInfo", channelUserInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存码商代理扩展
	 */
	@RequiresPermissions("channel:channelUserInfo:edit")
	@Log(title = "码商代理扩展", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ChannelUserInfo channelUserInfo)
	{		
		return toAjax(channelUserInfoService.updateChannelUserInfo(channelUserInfo));
	}
	
	/**
	 * 删除码商代理扩展
	 */
	@RequiresPermissions("channel:channelUserInfo:remove")
	@Log(title = "码商代理扩展", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(channelUserInfoService.deleteChannelUserInfoByIds(ids));
	}


	@GetMapping("/resetChannelSendPwd")
	public String resetBusinessPayPwd(ModelMap mmap) {
		mmap.put("user", ShiroUtils.getSysUser());
		String phonenumber = ShiroUtils.getSysUser().getPhonenumber();
		String phone_ = phonenumber.substring(0, 3) + "********" + phonenumber.substring(7, phonenumber.length());
		mmap.put("phonenumber",phone_);
		return "system/user/profile/resetChannelSendPwd";
	}

	@Log(title = "码商代理重置密码", businessType = BusinessType.UPDATE)
	@RequiresPermissions("channel:channelUserInfo:edit")
	@PostMapping("/resetChannelSendPwd")
	@ResponseBody
	public AjaxResult resetBusinessPayPwd(String oldPassword, String newPassword,String code) {
		User user = getSysUser();
		if (StringUtils.isNotEmpty(newPassword) && channelUserInfoService.matches(oldPassword,user.getUserId())) {
			return channelUserInfoService.resetSendPwd(user.getUserId() + "", newPassword,code);
		} else {
			return error("修改密码失败，旧密码错误");
		}
	}
	/**
	 * 重置密码
	 */
	@RequiresPermissions("channel:channelUserInfo:edit")
	@Log(title = "码商代理扩展", businessType = BusinessType.UPDATE)
	@PostMapping("/resetSendPwd")
	@ResponseBody
	public AjaxResult resetSendPwd(String id,String code) {
		return channelUserInfoService.resetSendPwd(id, Md5Utils.androidMD5("123456"),code);
	}

	@GetMapping("/checkPassword")
	@RequiresPermissions("channel:channelUserInfo:list")
	@ResponseBody
	public boolean checkPassword(String password) {
		if (channelUserInfoService.matches(password, ShiroUtils.getUserId())) {
			return true;
		}
		return false;
	}
}
